#include "ColladaHandler.h"
#include <xercesc\sax2\Attributes.hpp>
#include <iostream>

using namespace std;
void ColladaHandler::startElement(const   XMLCh* const    uri,
                            const   XMLCh* const    localname,
                            const   XMLCh* const    qname,
                            const   Attributes&     attrs)
{
    char* message = XMLString::transcode(localname);
    
	string s = message;
	if (s.compare("source")==0){
		//TODO: Make value dynamic so we get <geometry-id>-positions value
		string meshPositionsValue = "Cube-mesh-positions";
		string normalPositionsValue = "Cube-mesh-normals";
		string id = "id";
		string localName;
		string attrValue;
		// We will search for the Cube-mesh positions
		for(int i=0;i < attrs.getLength();i++){
			localName = XMLString::transcode(attrs.getLocalName(i));
			attrValue = XMLString::transcode(attrs.getValue(i));
			if (id.compare(localName)==0){
				if (meshPositionsValue.compare(attrValue)==0){
					this->isSourcePositions=true;
				}
				if (normalPositionsValue.compare(attrValue)==0){
					this->isSourceNormals=true;
				}
			}

		}
	}
	if(s.compare("polylist")==0){
		this->isPolyList = true;
	}
	if (this->isPolyList && s.compare("p")==0){
		this->isP = true;
	}
	if (this->isSourcePositions || this->isSourceNormals){
		if (s.compare("float_array") == 0){
			this->isFloatArray=true;
		}
	}
    XMLString::release(&message);
}

void ColladaHandler::characters(const XMLCh* const chars, const XMLSize_t length){
	
	if (this->isFloatArray){
		if (this->isSourcePositions){
			this->vertexesMsg = (char*)malloc(length);
			XMLString::transcode(chars,this->vertexesMsg,length);
		}
		if (this->isSourceNormals){
			this->normalsMsg = (char*)malloc(length);
			XMLString::transcode(chars,this->normalsMsg,length);
		}
	}
	if (this->isP){
		this->polyList = (char*)malloc(length);
		XMLString::transcode(chars,this->polyList,length);
	}
	
}

void ColladaHandler::endElement
(
	const XMLCh* const uri,
	const XMLCh* const localname,
	const XMLCh* const qname
){
	char* message = XMLString::transcode(localname);
   
	string s = message;
	if (s.compare("source") == 0){
		this->isSourcePositions=false;
		this->isSourceNormals=false;
	}
	if(s.compare("polylist")==0){
		this->isPolyList = false;
	}
	if (this->isPolyList && s.compare("p")==0){
		this->isP = false;
	}
	if ((this->isSourcePositions || this->isSourceNormals) && s.compare("float_array") == 0){
		this->isFloatArray=false;
	}
}

void ColladaHandler::fatalError(const SAXParseException& exception)
{
    char* message = XMLString::transcode(exception.getMessage());
    cout << "Fatal Error: " << message
         << " at line: " << exception.getLineNumber()
         << endl;
	
    XMLString::release(&message);
}

char* ColladaHandler::getVertexesMsg(){
	return this->vertexesMsg;
}

char* ColladaHandler::getNormalsMsg(){
	return this->normalsMsg;
}

char* ColladaHandler::getPolyList(){
	return this->polyList;
}